'use strict';
var redis = require('redis'),
    RDS_PORT = 6379, //端口号
    RDS_HOST = '127.0.1.1', //服务器IP
    RDS_PWD = 'assinsass',
    RDS_OPTS = {
        auth_pass: RDS_PWD
    },
    client = redis.createClient(RDS_PORT, RDS_HOST, RDS_OPTS);
// client = redis.createClient();
export default class extends think.service.base {
    /**
     * init
     * @return {}         []
     */
    init() {
        client.on('ready', function(err) {
            if (err) {
                throw 'redis ready Error';
            }
            console.log('redis start')
        });
    }
    async addWechatUserInfo(id, user) {
        client.SADD('wetchat' + id, user);
    }
    async getWeiUserInfo(id) {
        return new Promise(function(resolve, reject) {
            client.SMEMBERS("wetchat" + id, function(err, res) {
                if (err) {
                    console.log('Error:' + err);
                    reject(new Error())
                }
                resolve(res)
            });
        });
    }

    async add(name,value = 0) {

        client.set(name,value);
    }
    async updata(name, value = 1) {
        client.INCRBY(name, value);
    }
    async get(name) {
        return new Promise(function(resolve, reject) {
            client.get(name, (err, data) => {
                console.log('获取'+name)
                if (err) {
                    reject(new Error());
                }
                resolve(data);
            });
        });
    }

    async addDayHistory() {
        jedis.rpush("list", "b");
    }
    async setClearTime(name, time) {
        client.EXPIRE(name, time);
        client.ttl(name, (err, data) => {
            if (err) {
                console.log('redis err' + err)
            }
            console.log('redis data' + data)
        });
    }

    async getKeyClearTime(name, callback) {
        console.log('name' + name)
        client.ttl(name, callback);
    }
}
